package com.qfedu.aunt.commons.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.qfedu.aunt.commons.exception.BusinessException;
import com.qfedu.aunt.commons.pojo.EmployerEvaluation;
import com.qfedu.aunt.commons.mapper.EmployerEvaluationMapper;
import com.qfedu.aunt.commons.result.ResponseCode;
import com.qfedu.aunt.commons.service.EmployerEvaluationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.aunt.commons.vo.EmployerEvaluationVo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;

/**
 * <p>
 * 雇主评价 服务实现类
 * </p>
 *
 * @author xiaobobo
 * @since 2022-10-26
 */
@Service
public class EmployerEvaluationServiceImpl extends ServiceImpl<EmployerEvaluationMapper, EmployerEvaluation> implements EmployerEvaluationService {

    @Autowired
    private EmployerEvaluationMapper employerEvaluationMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(EmployerEvaluationVo employerEvaluationVo, HttpServletRequest req) throws InvocationTargetException, IllegalAccessException {
       if(null==employerEvaluationVo||
               StringUtils.isEmpty(employerEvaluationVo.getEvaluationContent())||
               employerEvaluationVo.getEvaluationType()==null||(StringUtils.isEmpty(employerEvaluationVo.getAgentId())&&StringUtils.isEmpty(employerEvaluationVo.getAuntId()))){
           throw new BusinessException(ResponseCode.PARAMETER_ERROR.getCode(),
                   ResponseCode.PARAMETER_ERROR.getMessage());
       }
       //程序执行到这里说明参数没有问题
        //接下来要获取token然后获取用户id
        String token = req.getHeader("token");
       if(StringUtils.isEmpty(token)){
          throw new BusinessException(ResponseCode.TOKEN_ERROR.getCode(),
                  ResponseCode.TOKEN_ERROR.getMessage());
       }
        //程序执行到这里 说明参数都没有问题
        //TODO 解析用户id
        String employerId="0001";
        employerEvaluationVo.setEmployerId(employerId);
        //将Vo装换成po对象
        EmployerEvaluation employerEvaluation = new EmployerEvaluation();
        BeanUtils.copyProperties(employerEvaluation,employerEvaluationVo);
        //接下来操作数据库
        employerEvaluationMapper.insert(employerEvaluation);
    }
}
